package com.bigcreate.service;

import com.bigcreate.dao.BookMapper;
import com.bigcreate.entity.Books;
import com.bigcreate.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService{

    @Autowired
    BookMapper bookMapper;

    public BookServiceImpl(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }


    @Override
    public int addBook(Books book) {
        return bookMapper.addBook(book);
    }

    @Override
    public int deleteBookById(int id) {
        return bookMapper.deleteBookById(id);
    }

    @Override
    public int updateBook(Books book) {
        if(book.getId()==0) return 0;
        return bookMapper.updateBook(book);
    }

    @Override
    public int updateBookNum(int id, int num, int type) {
        //原来的数量
        int origin = bookMapper.queryBookById(id).getQuantity();

        if(type>0){
            origin = origin+num;
        }
        else{
            origin = origin-num;
            if(origin < 0)
                origin = 0;
        }

        bookMapper.updateBookNum(id,origin);
        return origin;
    }

    @Override
    public Books queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }

    @Override
    public List<Books> queryAll() {
        return bookMapper.queryAll();
    }

    @Override
    public List<Books> queryBookByName(String name) {
        return bookMapper.queryBookByName(name);
    }

    @Override
    public User getUser(String name) {
        return bookMapper.getUser(name);
    }
}
